const request = require('../utils/request')
// const HOSTNAME = '120.79.61.53'
const HOSTNAME = '118.144.87.37'
// const PORT = 8100
const PORT = 13083
const updateFunc = require('../update')
const fs = require('fs')
const path = require('path')

window.electron = require('electron')

window.WebViewJavascriptBridge = {
    callHandler: (name, data, callback) => {
        switch (name) {
            case 'getH5RequestProxy':
                getH5RequestProxy(data, callback);
                break;
            case 'H5RequestProxy':
                H5RequestProxy(data, callback);
                break;
            case 'toZipPage':
                toZipPage(data, callback);
                break;
        }
    },
    registerHandler(name, data, callback) {
        switch (name) {
            case 'getModelId':
                getModelId(data, callback);
                break;
        }
    }
}

window.onload = function () {
    if (document.querySelector('span.back-btn')) {
        document.querySelector('span.back-btn').addEventListener('click', function () {
            const ipcRenderer = electron.ipcRenderer
            ipcRenderer.send('goBackHome', true)
        })
    }
}



function getH5RequestProxy(data, callback) {
    const config = {
        hostname: HOSTNAME, // test
        port: PORT, // test
        path: data.TradeCode,
        method: "GET",
        headers: {
            "Content-Type": "application/json;charset=utf-8"
        },
        timeout: 60000
    };
    const params = data.Param || JSON.stringify({})
    request(config, params)
        .then(res => {
            console.log('get result:', res)
            callback(JSON.parse(res))
        })
}

function H5RequestProxy(data, callback) {
    const config = {
        hostname: HOSTNAME, // test
        port: PORT, // test
        path: data.TradeCode,
        method: "POST",
        headers: {
            "Content-Type": "application/json;charset=utf-8",
        },
        timeout: 60000
    };
    console.log(data)
    const params = data.Param || JSON.stringify({})
    request(config, params)
        .then(res => {
            console.log('post result:', res)
            callback(JSON.parse(res))
        })
}

function toZipPage(data, callback) {
    data = data.name
    const ipcRenderer = electron.ipcRenderer
    try {
        const local = require('../zip/config.json')
        if (local.hasOwnProperty(data.name) && local[data.name].pkg_url === data.url) {
            ipcRenderer.send('changeUrl', local[data.name].local_path)
            console.log(1)
        } else {
            console.log(2)
            updateFunc(data.name, data.url, (err) => {
                if (err) {
                    throw new Error(err)
                }
                console.log('success')
                ipcRenderer.send('changeUrl', local[data.name].local_path)
            })
        }
        console.log(local)
    } catch (e) {
        updateFunc(data.name, data.url, (err) => {
            if (err) {
                throw new Error(err)
            }
            console.log('success')
            ipcRenderer.send('changeUrl', require('../zip/config.json')[data.name].local_path)
        })
    }
}

function getModelId(data, callback) {
    callback()
}